package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.GroupVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrMapper attrMapper;
    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;
    @Autowired
    SkuAttrValueMapper skuAttrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> queryCategorysByCid(Long cid) {
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrGroupEntity::getCategoryId, cid);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<AttrGroupEntity> queryAttrgroupsByCid(Long cid) {
        //1.根据分类id查询分组
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrGroupEntity::getCategoryId, cid);
        List<AttrGroupEntity> groupEntities = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(groupEntities)) {
            return null;
        }
        //2.遍历分组查询分组下的规格参数
        groupEntities.forEach(attrGroupEntity -> {
            LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AttrEntity::getGroupId, attrGroupEntity.getId());
            wrapper.eq(AttrEntity::getType, 1);
            List<AttrEntity> attrEntities = this.attrMapper.selectList(wrapper);
            attrGroupEntity.setAttrEntities(attrEntities);
        });
        return groupEntities;
    }

    @Override
    public List<GroupVo> queryGroupWithAttrValuesByCidAndSpuIdAndSkuId(Long cid, Long spuId, Long skuId) {
        //1.查询当前商品所属分类下有哪些规格参数分组
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrGroupEntity::getCategoryId, cid);
        List<AttrGroupEntity> attrGroupEntities = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(attrGroupEntities)) {
            return null;
        }
        //2.遍历group集合获取每个分组下的规格参数
        return attrGroupEntities.stream().map(attrGroupEntity -> {
            GroupVo groupVo = new GroupVo();
            // 设置分组的id和name
            groupVo.setGroupId(attrGroupEntity.getId());
            groupVo.setGroupName(attrGroupEntity.getName());
            //3.查询每个组下的规格参数
            LambdaQueryWrapper<AttrEntity> query = new LambdaQueryWrapper<>();
            query.eq(AttrEntity::getGroupId, attrGroupEntity.getId());
            List<AttrEntity> attrEntities = this.attrMapper.selectList(query);
            if (CollectionUtils.isEmpty(attrEntities)) {
                return groupVo;
            }
            // 获取attrIds集合
            List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
            List<AttrValueVo> attrs = new ArrayList<>();
            //4.查询基本类型规格参数及值
            List<SpuAttrValueEntity> spuAttrValueEntities = this.spuAttrValueMapper.selectList(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in("attr_id", attrIds));
            if (!CollectionUtils.isEmpty(spuAttrValueEntities)) {
                List<AttrValueVo> attrValueVos = spuAttrValueEntities.stream().map(spuAttrValueEntity -> {
                    AttrValueVo attrValueVo = new AttrValueVo();
                    BeanUtils.copyProperties(spuAttrValueEntity, attrValueVo);
                    return attrValueVo;
                }).collect(Collectors.toList());
                attrs.addAll(attrValueVos);
            }
            //5.查询销售类型的规格参数及值
            List<SkuAttrValueEntity> skuAttrValueEntities = this.skuAttrValueMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", attrIds));
            if (!CollectionUtils.isEmpty(skuAttrValueEntities)) {
                attrs.addAll(skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                    AttrValueVo attrValueVo = new AttrValueVo();
                    BeanUtils.copyProperties(skuAttrValueEntity, attrValueVo);
                    return attrValueVo;
                }).collect(Collectors.toList()));
            }
            groupVo.setAttrs(attrs);
            return groupVo;
        }).collect(Collectors.toList());
    }
}














